<template>
	<view class="content">
		<image class="logo" src="../../static/images/logo.png"></image>
		<image class="scan" src="../../static/images/scan.png"></image>
		<view class="scan-button" @click="doScan">点击开始扫码</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				content: '',
				url: '',
				scanning: false
			}
		},
		onLoad() {

		},
		methods: {
			doScan() {
				if (this.scanning) {
					return;
				}
				this.scanning = true;
				uni.scanCode({
					onlyFromCamera: true,
					success: res => {
						this.url = res.result;
						let url = this.getUrl();
						let typename = this.getQueryVariable('typename');
						let errid = this.getQueryVariable('errid');
						let sn = this.getQueryVariable('sn');
						// 校验三者参数是否齐全，不齐全重新扫
						if (!typename || !errid || !sn) {
							uni.showToast({
								title: '二维码参数有误\r\n',
								duration: 2000,
								icon: 'none'
							});
							return;
						}
						uni.showToast({
							title: '扫描成功',
							duration: 2000							
						});
						setTimeout(() => {
							this.scanning = false;
							uni.navigateTo({
								url: '/pages/feedback/feedback?url=' + url + '&sn=' + sn + '&typename=' + typename + '&errid=' + errid 
							})
						}, 2000)
					},
					fail: err => {
						this.scanning = false;
					}
				});
			},
			/**
			 * 获取url请求，不包括参数
			 */
			getUrl() {
				let urlArr = this.url.split("?");
				return urlArr[0];
			},
			/**
			 * 获取url参数
			 * @param {Object} param 参数名
			 */
			getQueryVariable(param) {
				let urlArr = this.url.split("?");
				if (urlArr.length === 1 || !urlArr[1]) {
					return;
				}
				let query = urlArr[1];
				let vars = query.split("&");
				for (let i = 0; i < vars.length; i++) {
					let pair = vars[i].split("=");
					if (pair[0] == param) {
						return pair[1];
					}
				}
				return; 
			}
		}
		
	}
</script>

<style scoped lang="scss">
	 @import '~@/static/css/index.scss';
</style>
